package q148_sortList;

import CommonClass.Common.ListNode;

import java.util.ArrayList;
import java.util.Collections;

public class Solution_1 {
    /*
    暴力链表排序
    直接使用一个ArrayList存储每一个节点值
    然后再次遍历重新赋值
     */
    public ListNode sortList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        ArrayList<Integer> list = new ArrayList<>();
        ListNode dummy = new ListNode(0, head);
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }

        Collections.sort(list);
        head = dummy.next;
        for (Integer integer : list) {
            head.val = integer;
            head = head.next;
        }

        return dummy.next;


    }
}
